import puppeteer from 'puppeteer';
import fs from 'fs';


const browser = await puppeteer.launch({
  headless: false,
  defaultViewport: null,
  // args: ['--start-maximized'],
  ignoreDefaultArgs: ['--enable-automation']
});

const page = await browser.newPage();
await page.goto('https://u.gitcafe.net/#hyds');
const table_items = await page.$$('.tableizer-table.key_nav tr td:not(:first-child) a');
let data = [];
console.log(table_items.length);

async function scrap() {
  // for await (let item of table_items) {
  //   item.click();
  //   await page.waitForTimeout(1000);
  //   await page.waitForSelector('.GeneratedTable.list tbody tr');
  //   const tabData = await page.$$eval('.GeneratedTable.list tbody tr', els => els.map(el => {
  //     return {
  //       data: el.innerText,
  //       tr: el.innerHTML,
  //     };
  //   }));
  //   // console.log(tabData);
  //   data = data.concat(tabData);
  //   await page.waitForTimeout(2000);
  // }
  console.log(table_items[1]);
  table_items[1].click();
    await page.waitForTimeout(1000);
    await page.waitForSelector('.GeneratedTable.list tbody tr');
    const tabData = await page.$$eval('.GeneratedTable.list tbody tr', els => els.map(el => {
      return {
        data: el.innerText,
        tr: el.innerHTML,
      };
    }));
    // console.log(tabData);
    data = data.concat(tabData);
    await page.waitForTimeout(2000);
  



  fs.writeFileSync('data.json', JSON.stringify(data));

}


await scrap();

